package com.flow.demo.module.gd.controller.admin.activities;

import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import java.util.*;
import java.io.IOException;

import com.flow.demo.framework.common.pojo.PageParam;
import com.flow.demo.framework.common.pojo.PageResult;
import com.flow.demo.framework.common.pojo.CommonResult;
import com.flow.demo.framework.common.util.object.BeanUtils;
import static com.flow.demo.framework.common.pojo.CommonResult.success;

import com.flow.demo.framework.excel.core.util.ExcelUtils;

import com.flow.demo.framework.operatelog.core.annotations.OperateLog;
import static com.flow.demo.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.flow.demo.module.gd.controller.admin.activities.vo.*;
import com.flow.demo.module.gd.dal.dataobject.activities.ActivitiesDO;
import com.flow.demo.module.gd.service.activities.ActivitiesService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Tag(name = "管理后台 - 活动")
@RestController
@RequestMapping("/gd/activities")
@Validated
public class ActivitiesController {

    @Resource
    private ActivitiesService activitiesService;

    @PostMapping("/create")
    @Operation(summary = "创建活动")
    @PreAuthorize("@ss.hasPermission('gd:activities:create')")
    public CommonResult<Integer> createActivities(@Valid @RequestBody ActivitiesSaveReqVO createReqVO) {
        return success(activitiesService.createActivities(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新活动")
    @PreAuthorize("@ss.hasPermission('gd:activities:update')")
    public CommonResult<Boolean> updateActivities(@Valid @RequestBody ActivitiesSaveReqVO updateReqVO) {
        activitiesService.updateActivities(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除活动")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('gd:activities:delete')")
    public CommonResult<Boolean> deleteActivities(@RequestParam("id") Integer id) {
        activitiesService.deleteActivities(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得活动")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('gd:activities:query')")
    public CommonResult<ActivitiesRespVO> getActivities(@RequestParam("id") Integer id) {
        ActivitiesDO activities = activitiesService.getActivities(id);
        return success(BeanUtils.toBean(activities, ActivitiesRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得活动分页")
    @PreAuthorize("@ss.hasPermission('gd:activities:query')")
    public CommonResult<PageResult<ActivitiesRespVO>> getActivitiesPage(@Valid ActivitiesPageReqVO pageReqVO) {
        PageResult<ActivitiesDO> pageResult = activitiesService.getActivitiesPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ActivitiesRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出活动 Excel")
    @PreAuthorize("@ss.hasPermission('gd:activities:export')")
    @OperateLog(type = EXPORT)
    public void exportActivitiesExcel(@Valid ActivitiesPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ActivitiesDO> list = activitiesService.getActivitiesPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "活动.xls", "数据", ActivitiesRespVO.class,
                        BeanUtils.toBean(list, ActivitiesRespVO.class));
    }

}